package ro.gateway.aida.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.Utils;


/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: ActivityObjectsOptionsAction.java,v 1.1 2004/10/24 23:37:18 mihaipostelnicu Exp $
 * @deprecated
 */
public class ActivityObjectsOptionsAction extends Action {
    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it),
     * with provision for handling exceptions thrown by the business logic.
     * Return an {@link ActionForward} instance describing where and how
     * control should be forwarded, or <code>null</code> if the response
     * has already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception Exception if the application business logic throws
     *  an exception
     * @since Struts 1.1
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        PersistenceToken token = PersistenceManager.tokenLookup(getServlet()
                                                                    .getServletContext());
        User logged_user = UserUtils.getLogged(request);

        if ((logged_user == null) ||
                (!Arbiter.getManager(token).canDo(request,
                    Perms.ADMIN_TOOLS)))
            return mapping.findForward("norights");

        ActivityObjectsOptions aoo_inst = ActivityObjectsOptions.getInstance();
        ActivityObjectsOptionForm aoof_form = (ActivityObjectsOptionForm) form;

        String action = request.getParameter("action");

        if (action == null)
            action = "edit";

//        System.out.println("action:" + action);

        if ("edit".equals(action)) {
//            System.out.println("action:edit");
            Utils.setBreadCrumbs(request, "bc.objconf");
            return mapping.findForward("edit");
        } else if ("save".equals(action)) {
            String[] keys = aoo_inst.getRegisteredObjects();

            for (int i = 0; i < keys.length; i++) {
                String key = keys[i];
                boolean enabled = request.getParameter("en" + key) != null;
                boolean mandatory = request.getParameter("ma" + key) != null;
                aoo_inst.setEnabled(key, enabled);
                aoo_inst.setMandatory(key, enabled & mandatory);
            }

              aoo_inst.saveToDB( token );
//            try {
//                aoo_inst.saveToFile(getServlet().getServletContext()
//                                        .getRealPath("/WEB-INF/aoof.properties"));
//            } catch (IOException ioEx) {
//                ioEx.printStackTrace();
//            }
        }

        return mapping.findForward("finnish");
    }
}
